﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

using SilverApp.View;

namespace SilverApp.FileTransfert
{
    public class ChunkSender
    {
        private LiveService.ChunksInfo  sendinfos;
        private List<byte[]>            sendlist;
        private System.Guid             token;
        private int                     nbchunks = 0;
        private int chunksize = 15000;
        private int sended = 0;
        public string finalmsg = "no send error";
        private FileUploadWindow upW;

        public ChunkSender(System.Guid pid, LiveService.File info, System.Guid idUser)
        {
            token = System.Guid.NewGuid();
            sendinfos = new LiveService.ChunksInfo();
            sendinfos.Parentid = pid;
            sendinfos.Fileinfo = info;
            sendinfos.Userid = idUser;
            sendinfos.Token = token;
        }

        public void InitializeChunkList(byte[] basedatas)
        {
            try
            {
                this.sendlist = new List<byte[]>();
                nbchunks = (int) Math.Ceiling(((double)basedatas.Length / (double) chunksize));
                int start = 0;

                for (int i = 0; i < nbchunks; i++)
                {
                    byte[] barray;

                    start = i * chunksize;

                    if (start + chunksize > basedatas.Length)
                    {
                        barray = new byte[basedatas.Length - start];
                    }
                    else
                    {
                       barray = new byte[chunksize];
                    }

                    Array.Copy(basedatas, start, barray, 0, barray.Length);
                    this.sendlist.Add(barray);
                }
                this.finalmsg = "Bytes parsed : " + this.sendlist.Count.ToString();
            }
            catch (Exception ex)
            {
                this.finalmsg = "Sender upload error : " + ex.Message;
            }
        }

        public double NbChunks
        {
            get { return nbchunks; }
        }

        public FileUploadWindow UpWin
        {
            get { return this.upW; }
            set { this.upW = value; }
        }

        public void UploadChunk()
        {
            if (this.sendinfos == null)
            {
                this.finalmsg = "[INFOS NULL]";
                upW.Close();
            }
            else
            {
                try
                {
                    if (sended < this.sendlist.Count)
                    {
                        LiveService.LServiceClient upcli = new LiveService.LServiceClient();
                        upcli.ReceiveUploadChunkCompleted +=
                                     new EventHandler<LiveService.ReceiveUploadChunkCompletedEventArgs>(FileUploadNext);
                        upcli.ReceiveUploadChunkAsync(this.sendlist[this.sended], this.sendinfos.Parentid, this.sendinfos.Fileinfo, this.sendinfos.Userid);
                    }
                }
                catch (Exception ex)
                {
                    this.finalmsg = "Sender upload error : " + ex.Message;
                }
            }
        }

        public void FileUploadNext(object sender, LiveService.ReceiveUploadChunkCompletedEventArgs e)
        {
            try
            {
                if (e.Result == true)
                {
                    sended += 1;
                    if (sended < nbchunks && sended < this.sendlist.Count)
                        UploadChunk();
                    else
                    {
                        this.finalmsg = "upload done";
                        upW.Close();
                    }
                }
                else
                {
                    this.finalmsg = "error in upload";
                    upW.Close();
                }
            }
            catch (Exception ex)
            {
                this.finalmsg = "Sender upload error : " + ex.Message;
            }
        }
    }
}
